package bingo.codeweaver.core.metadata;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

import net.bingosoft.common.util.ClassUtil;
import net.bingosoft.common.util.StringHelper;
import bingo.codeweaver.core.annotation.Table;
import bingo.codeweaver.core.strategy.DefaultNamingStrategy;
import bingo.codeweaver.core.strategy.NamingStrategy;


/**
 * Mar 6, 2009 6:38:39 AM Table.java
 * Author:yangdongyu(yangdy@bingosoft.net)
 * ReadMe:
 * ChangeLog:
 */
public class TableMetadata {
	private String name;
	private String schema;
	private List<String> primaryKeys = new ArrayList<String>(3);
	private String comments = "";
	private DatabaseMetadata database;
	private List<ColumnMetadata> columns = new ArrayList<ColumnMetadata>();
	private NamingStrategy namingStrategy = new DefaultNamingStrategy();

	public TableMetadata() {
	}

	public TableMetadata(Class<?> entity) {
		if(entity.isAnnotationPresent(Table.class)){
			Table table = entity.getAnnotation(Table.class);
			//获取表名
			name = StringHelper.isEmpty(table.name()) ? namingStrategy.toTableName(entity.getSimpleName()) : table.name();
			if(StringHelper.isNotEmpty(table.comment())){
				comments = table.comment();
			}
		}else{
			name = namingStrategy.toTableName(entity.getSimpleName());
		}
		Field[] fieldArray = entity.getDeclaredFields() ;
		for(Field f : fieldArray){
			if(f.getType() == String.class || ClassUtil.isPrimitiveOrWrapper(f.getType())){
				if(!(Modifier.isStatic(f.getModifiers()) || Modifier.isFinal(f.getModifiers()) )){
					addColumn(new ColumnMetadata(this,f));
				}
			}
		}
	}


	public String getClassName(){
		return this.getNamingStrategy().toClassName(name);
	}
	public NamingStrategy getNamingStrategy(){
		if(this.database.getNamingStrategy() != null){
			return this.database.getNamingStrategy();
		}else{
			return this.namingStrategy;
		}

	}

	public List<ColumnMetadata> getColumns() {
		return columns;
	}
	public void setColumns(List<ColumnMetadata> columns) {
		this.columns = columns;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSchema() {
		return schema;
	}
	public void setSchema(String schema) {
		this.schema = schema;
	}
	public List<String> getPrimaryKeys() {
		return primaryKeys;
	}
	public void setPrimaryKeys(List<String> primaryKeys) {
		this.primaryKeys = primaryKeys;
	}

	public String getComments() {
		return comments;
	}
	public void setComments(String comments) {
		this.comments = comments;
	}
	public DatabaseMetadata getDatabase() {
		return database;
	}
	public void setDatabase(DatabaseMetadata database) {
		database.addTable(this);
		this.database = database;

	}

	public TableMetadata addColumn(ColumnMetadata c){
		columns.add(c);
		if(c.isPrimaryKey() && !primaryKeys.contains(c.getName())){
			primaryKeys.add(c.getName());
		}
		return this;
	}

	public TableMetadata addPrimaryKey(String key){
		this.primaryKeys.add(key);
		getColumnMetadata(key).setPrimaryKey(true);
		return this;
	}

	public ColumnMetadata getColumnMetadata(String name){
		for(ColumnMetadata c : this.columns){
			if(name.equals(c.getName())){
				return c;
			}
		}
		return null;
	}

	public String toString(){
		return this.name;
	}



}
